<?php
	$path = "../";
	include_once($path.'configuration.php');
	/*Traitement*/
	include_once($path.'head.php');
	
	if(isset($_GET['action']))
		$action = $_GET['action'];
	else
		$action='add';
		
	if($action=="add"){
		if(isset($_POST) && isset($_POST['submit']) && $_POST['submit'] != ''){
			extract($_POST);
			if(isset($machine) && $machine !='' && isset($type) && $type !='' && isset($complement) && $complement !=''  && isset($resolution) && $resolution !=''){
				$incident = new Incident($dbh);
				$incident->setMachine($machine);
				$incident->setType($type);
				$incident->setComplement($complement);
				$incident->setIs_resolved($resolution);				
				$incident->save();
				header('Location: '.$path . 'administration/gestion_incident.php');
				exit;
			}else{
				$message = "Veuillez remplir tous les champs obligatoires !";
			}
		}
	}elseif($action == "modif"){
			if(isset($_GET) && isset($_GET['incident']) && $_GET['incident'] !=0){
				if(isset($_POST) && isset($_POST['submit']) && $_POST['submit'] != ''){
					extract($_POST);
					if(isset($machine) && $machine !='' && isset($complement) && $complement !=''  && isset($resolution) && $resolution !=''){
						$incident = new Incident($dbh, $_GET['incident']);
						$incident->setMachine($machine);
						$incident->setType($type);
						$incident->setComplement($complement);
						$incident->setIs_resolved($resolution);
						$incident->save();
						//header('Location: '.$path .'administration/gestion_incident.php');
					}else{
						$message = "Veuillez remplir tous les champs obligation !";
					}
				}
			}
	}elseif($action == "delete"){
		if(isset($_GET) && isset($_GET['incident']) && $_GET['incident'] != 0){
			$incident = new Incident($dbh, $_GET['incident']);
			$incident->delete();
			header('Location: '.$path .'administration/gestion_incident.php');
		}
	}
	if($action == "modif"){
		if(isset($_GET) && isset($_GET['incident']) && $_GET['incident'] !=0){
			$incident = new Incident($dbh, $_GET['incident']);
			$machine = $incident->getMachine();
			$type = $incident->getType();
			$complement = $incident->getComplement();
			$date_detection = $incident->getDate_detection();
			$date_resolution = $incident->getDate_resolution();
		}
	}
?>

<div id="bodyWrapper">
	<form method="post" id="form" action="">
		<p>
			<label for="machine" class="label">Machine* :</label>
			<input type="text" id="machine" name="machine" class="input" value="<?php if(isset($machine)) echo $machine;?>" />
		</p>
		<p>
			<label for="type" class="label">Type :</label>
			<input type="text" id="type" name="type" class="input" value="<?php if(isset($type)) echo $type; ?>" />
		</p>
		<p>
			<label for="complement" class="label">Complement* :</label>
			<input type="text" id="complement" name="complement" class="input" value="<?php if(isset($complement)) echo $complement; ?>" />
		</p>
		<p>
			<label for="resolution" class="label">Resolu :</label>
			<select name="resolution" id="resolution" class="input"> 
				<option value="true">Oui</option>
				<option value="false">Non</option>
			</select>
		</p>
		<p>
			<input type="submit" name="submit" id="submit" class="input" value="Envoyer" />
		</p>
		
		
	</form>
</div>
<?php
include_once($path.'footer.php');
?>